package org.jeecg.modules.ws.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.ws.dto.ExportSheetDataDto;
import org.jeecg.modules.ws.dto.UploadSheetDataDto;
import org.jeecg.modules.ws.entity.WsBondInfo;
import org.jeecg.modules.ws.entity.WsBondProject;
import org.jeecg.modules.ws.entity.WsProjectManagement;
import org.jeecg.modules.ws.entity.WsProjectSub;
import org.jeecg.modules.ws.mapper.WsProjectManagementMapper;
import org.jeecg.modules.ws.service.IWsProjectManagementService;
import org.jeecg.modules.ws.service.IWsProjectSubService;
import org.jeecg.modules.ws.util.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * @Description: 项目管理
 * @Author: jeecg-boot
 * @Date:   2025-03-31
 * @Version: V1.0
 */
@Service
public class WsProjectManagementServiceImpl extends ServiceImpl<WsProjectManagementMapper, WsProjectManagement> implements IWsProjectManagementService {
    @Autowired
    private IWsProjectSubService projectSubService;
    @Autowired
    private ExcelUtil excelUtil;

    @Override
    public Result<?> uploadExcel(MultipartFile file, HttpServletRequest request, HttpServletResponse response) {
        List<String> list1 = new ArrayList<>();
        list1.add("project_code");
        List<String> list2 = new ArrayList<>();
        list2.add("project_code");
        list2.add("sub_project_code");
        List<UploadSheetDataDto<?>> sheetDataList = new ArrayList<>();
        sheetDataList.add(new UploadSheetDataDto<WsProjectManagement>(WsProjectManagement.class, this, list1));
        sheetDataList.add(new UploadSheetDataDto<WsProjectSub>(WsProjectSub.class, projectSubService, list2));
        return excelUtil.importExcel(request, response, sheetDataList);
    }

    @Override
    public ModelAndView exportSampleExcel(HttpServletRequest request, WsProjectManagement wsProjectManagement) {
        QueryWrapper<WsProjectManagement> queryWrapper = QueryGenerator.initQueryWrapper(wsProjectManagement, request.getParameterMap());
        List<WsProjectManagement> list1 = this.list(queryWrapper);
        List<WsProjectSub> list2 = new ArrayList<>();
        list1.forEach(projectManagement -> {
            String projectCode = projectManagement.getProjectCode();
            List<WsProjectSub> bondProjectList = projectSubService.queryByProjectCode(projectCode);
            list2.addAll(bondProjectList);
        });
        /*Map<List, Class> map = new LinkedHashMap<>();
        map.put(list1, WsBondInfo.class);
        map.put(list2, WsBondProject.class);*/
        List<ExportSheetDataDto<?>> sheetDataList = new ArrayList<>();
        sheetDataList.add(new ExportSheetDataDto<WsProjectManagement>(list1, WsProjectManagement.class,"项目工程主体"));
        sheetDataList.add(new ExportSheetDataDto<WsProjectSub>(list2, WsProjectSub.class,"子项目工程"));
        List xls = excelUtil.packageSheet(sheetDataList);
        return excelUtil.exportXls("项目管理",xls);
    }
}
